﻿using AccountApi.Entitys;
using AccountApi.tools;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace AccountApi.Models
{
    public class AccountDBModel
    {
        #region 单例
        private static object lock_object = new object();
        private static AccountDBModel instance = null;
        public static AccountDBModel Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lock_object)
                    {
                        if (instance == null)
                        {
                            instance = new AccountDBModel();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion
        private const string connStr = "server=localhost;port=3306;user id=root;password=tang1990;database=argp_database";

        public AccountEntity Get(int id) {
            string connStr = "server=localhost;port=3306;user id=root;password=tang1990;database=argp_database";
            MySqlParameter p1 = new MySqlParameter("@id", MySqlDbType.Int32);
            p1.Value = id;
            MySqlParameter[] paras = {p1};
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                conn.Open();
                MySqlCommand comm = new MySqlCommand("call GetAccountForId(@id);", conn);
                comm.Parameters.Add(new MySqlParameter("@id", id));

                using (MySqlDataReader dr = comm.ExecuteReader())
                {
                    AccountEntity entity = new AccountEntity();
                    if (dr.Read())
                    {
                        entity.Id = dr["Id"] == null ? 0 : Convert.ToInt32(dr["Id"]);
                        entity.UserName = dr["UserName"] == null ? string.Empty : dr["UserName"].ToString();
                        entity.Pwd = dr["Pwd"] == null ? string.Empty : dr["Pwd"].ToString();
                        entity.YuanBao = dr["YuanBao"] == null ? 0 : Convert.ToInt32(dr["YuanBao"]);
                        entity.LastServerId = dr["LastServerId"] == null ? 0 : Convert.ToInt32(dr["LastServerId"]);
                        entity.LastServerName = dr["LastServerName"] == null ? string.Empty : dr["LastServerName"].ToString();
                        entity.CreateTime = dr["CreateTime"] == null ? DateTime.Now : Convert.ToDateTime(dr["CreateTime"]);
                        entity.UpdateTime = dr["UpdateTime"] == null ? DateTime.Now : Convert.ToDateTime(dr["UpdateTime"]);
                    }
                    return entity;
                }
            }
        }

        public int Register(string userName,string pwd) {
            int userId = -1;
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                conn.Open();
                MySqlCommand comm = new MySqlCommand("call Account_Register(@name,@pwd);", conn);
                comm.Parameters.Add(new MySqlParameter("@name", userName));
                comm.Parameters.Add(new MySqlParameter("@pwd", pwd));
                //返回第一列的值
                userId = Convert.ToInt32(comm.ExecuteScalar().ToString());
            }
            return userId;
        }
    }
}